<template>
	<view>
		<image :style="{ width: width + 'rpx', height: height + 'rpx' }" v-if="src" :src="src" :class="imgClass" @click="chooseImage" mode="aspectFill"></image>
		<view :style="{ width: width + 'rpx', height: height + 'rpx' }" v-else :class="imgClass" class="imgUpload flexc justify-center" @click="chooseImage">
			<uni-icons type="plusempty" size="30" color="#dcdbdb"></uni-icons>
		</view>
		<!-- <image :style="{ width: width + 'rpx', height: height + 'rpx' }" v-else :src="imgSrc" :class="imgClass" @click="chooseImage"></image> -->
		<!-- <image src="" mode=""></image> -->
	</view>
</template>

<script>
	import {
		baseUrl
	} from '@/config/config.js'
	import uniIcons from "@/components/uni-icons/uni-icons"
	export default {
		components:{
			uniIcons
		},
		data() {
			return {
				src: null,
				imgSrc:require('../../static/new/xxaddimg.png')
			};
		},
		props: {
		
			imgClass: {
				type: String,
				default: ''
			},
			uploadImgId: {
				type: String,
				default: ''
			},
			value: {
				type: String,
				default: ''
			},
			width: {
				type: [Number,String],
				default: '180'
			},
			height: {
				type: [Number,String],
				default: '180'
			},
		},
		watch:{
			value(val){
				if(val){
					
					this.src=val
					console.log(this.src)
				}
				
			}
		},
		mounted(){
			this.src=this.value
			console.log(this.src)
		},
		methods: {
			chooseImage() {
				let that = this
				if (that.uploadImgId) {
					that.$emit('setUploadImgId', that.uploadImgId)
				}
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera','album'], //从相册选择
					success: function(res) {
						uni.uploadFile({
							url: baseUrl + '/common/upload',
							filePath: res.tempFilePaths[0],
							name: 'file',
							formData: {},
							// header: {
							// 	'Authorization':that.tui.getToken()? "Bearer "+that.tui.getToken():""
							// },
							success: (uploadFileRes) => {
								let data = JSON.parse(uploadFileRes.data)
								console.log(data,'----')
								if (data.code !== 0) return
								let result = data.url
							
								that.src = result;
								that.$emit('input', result);
								that.$emit('uploadSuccess', result);
							}
						})
					}
				})

			}
		},
		mounted() {
			// console.log('imgSrc', this.imgSrc)
		}
	}
</script>
<style>
@import url('@/css/common.css');
</style>
<style lang="scss">
	// image {
	// 	height: 130upx;
	// 	width: 130upx;
	// 	margin: 20upx 0;
	// }
	.imgUpload{
		border:2px dashed #dcdbdb;
		border-radius: 10rpx;
	}
</style>
